package com.stylefeng.guns.modular.vpn_business_info.service.impl;

import com.stylefeng.guns.core.common.constant.DatasourceEnum;
import com.stylefeng.guns.core.mutidatasource.annotion.DataSource;
import com.stylefeng.guns.modular.system.model.VpnBusinessDataInfo;
import com.stylefeng.guns.modular.system.dao.VpnBusinessDataInfoMapper;
import com.stylefeng.guns.modular.vpn.utils.ExportFileUtil;
import com.stylefeng.guns.modular.vpn.utils.PoiExcelUtil;
import com.stylefeng.guns.modular.vpn_business_info.service.IVpnBusinessDataInfoService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author stylefeng
 * @since 2020-03-09
 */
@Service
public class VpnBusinessDataInfoServiceImpl extends ServiceImpl<VpnBusinessDataInfoMapper, VpnBusinessDataInfo> implements IVpnBusinessDataInfoService {
    @Autowired
    private VpnBusinessDataInfoMapper businessDataInfoMapper;

    /**
     * 拉新用户数据
     * @return
     */
    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public List<Map<String, Object>> findNewUserList(String beginTime,String endTime) {
        return businessDataInfoMapper.findNewUserList(beginTime,endTime);
    }

    /**
     * 留存用户数据
     * @return
     */
    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public List<Map<String, Object>> findRetentionUserList(String beginTime,String endTime) {
        return businessDataInfoMapper.findRetentionUserList(beginTime,endTime);
    }

    /**
     * 财务数据信息
     * @return
     */
    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public List<Map<String, Object>> findFinancialInfo(String beginTime,String endTime) {
        return businessDataInfoMapper.findFinancialList(beginTime,endTime);
    }

    /**
     * 导出表格数据
     * @param type 查询类型：1-拉新，2-留存，3-财务数据
     * @param beginTime
     * @param endTime
     * @param response
     */
    @Override
    @DataSource(name = DatasourceEnum.DATA_SOURCE_BIZ)
    public void exportExcel(String type,String beginTime,String endTime,
                            HttpServletResponse response) {
        List<Map<String, Object>> dataList = null;
        List<String> headerList = new ArrayList<>();
        String sheetName = "业务数据";
        if(type.equals("1")){
            sheetName = "拉新用户核心指标";
            dataList = businessDataInfoMapper.findNewUserList(beginTime,endTime);
            headerList.add("日期");
            headerList.add("当日下载人数");
            headerList.add("月累计下载人数");
            headerList.add("当日注册人数");
            headerList.add("累计注册人数");
            headerList.add("当日注册及付费人数");
            headerList.add("IOS注册人数");
            headerList.add("安卓注册人数");
            headerList.add("网页注册人数");
        }
        else if(type.equals("2")){
            sheetName = "用户留存核心指标";
            dataList = businessDataInfoMapper.findRetentionUserList(beginTime,endTime);
            headerList.add("日期");
            headerList.add("多设备上线人数");
            headerList.add("总上线用户人数");
            headerList.add("总击上线次数");
            headerList.add("账号平均上线时长（分钟）");
            headerList.add("设备平均上线时长（分钟）");
        }
        else if(type.equals("3")){
            sheetName = "财务数据";
            dataList = businessDataInfoMapper.findFinancialList(beginTime,endTime);
            headerList.add("日期");
            headerList.add("当天购买人数");
            headerList.add("当天购买总金额（HKD）");
            headerList.add("当天未购买人数");
            headerList.add("当天购买7天套餐人数");
            headerList.add("当天购买30天套餐人数");
            headerList.add("当天购买90天套餐人数");
            headerList.add("当天购买180天套餐人数");
            headerList.add("当天购买360天套餐人数");
            headerList.add("7天套餐总用户数");
            headerList.add("30天套餐总用户数");
            headerList.add("90天套餐总用户数");
            headerList.add("180天套餐总用户数");
            headerList.add("360天套餐总用户数");
            headerList.add("未购买总人数");
            headerList.add("总用户付费率");
        }

        HSSFWorkbook workbook = new HSSFWorkbook();
        //创建一个Excel表单，参数为sheet的名字
        HSSFSheet sheet = workbook.createSheet(sheetName);

        //创建表头
        PoiExcelUtil.setTableHeader(workbook,sheet,headerList);
        //新增数据行，并且设置单元格数据
        if(type.equals("1")){
            ExportFileUtil.exportNewUserInfo(dataList,sheet);
        }
        else if(type.equals("2")){
            ExportFileUtil.exportRetentionUserInfo(dataList,sheet);
        }
        else if(type.equals("3")){
            ExportFileUtil.exportFinancialInfo(dataList,sheet);
        }

        String fileName = "业务数据-"+System.currentTimeMillis()+".xls";
        //写入文件并将文件已二进制流的方式返回为调用者
        PoiExcelUtil.writeWorkBook(response,workbook,fileName);
    }
}
